package org.dicr.util.jds.conn;

import java.util.prefs.*;

import org.dicr.prefs.*;

import com.borland.dx.sql.dataset.*;

/***********************************************************************************************************************
 * @author Igor A Tarasov me@dicr.dn.ua
 * @version 030102
 **********************************************************************************************************************/
public class ConnMisc {
	/*******************************************************************************************************************
	 * Конфигурирует ConnectionDescriptor из Preferences Если desc == null создается новый
	 * 
	 * @param pref preferences
	 * @param connDesc connection descriptor
	 * @return connection descriptor
	 * @throws BackingStoreException
	 ******************************************************************************************************************/
	public static ConnectionDescriptor getConnectionDescriptor(Preferences pref, ConnectionDescriptor connDesc) throws BackingStoreException {
		if (pref == null) throw new IllegalArgumentException("pref==null");
		ConnectionDescriptor desc = (connDesc != null) ? connDesc : new ConnectionDescriptor("");
		desc.setDriver(pref.get("Driver", "com.borland.datastore.jdbc.DataStoreDriver"));
		desc.setConnectionURL(pref.get("URL", "jdbc:borland:dslocal://tmp/database.jds"));
		desc.setUserName(pref.get("UserName", "user"));
		desc.setPassword(pref.get("Password", "password"));
		desc.setPromptPassword(pref.getBoolean("PromptPassword", false));
		desc.setProperties(PrefsUtils.getProperties(pref.node("Properties")));
		return desc;
	}

	/*******************************************************************************************************************
	 * Создает ConnectionElement из Preferences
	 * 
	 * @param pref preferences
	 * @return connection element
	 * @throws BackingStoreException
	 ******************************************************************************************************************/
	public static ConnectionElement getConnectionElement(Preferences pref) throws BackingStoreException {
		if (pref == null) throw new IllegalArgumentException("pref==null");
		ConnectionElement elem = new ConnectionElement(pref.get("Name", "Соединение"), getConnectionDescriptor(pref,
		        null));
		elem.setID(Long.parseLong(pref.name()));
		return elem;
	}

	/*******************************************************************************************************************
	 * Устанавливает Preferences из ConnectionDescriptor
	 * 
	 * @param pref preferences to set
	 * @param desc connection descriptor
	 ******************************************************************************************************************/
	public static void setPreferences(Preferences pref, ConnectionDescriptor desc) {
		if (desc == null || pref == null) throw new IllegalArgumentException("pref==null");
		if (desc.getDriver() != null) pref.put("Driver", desc.getDriver());
		if (desc.getConnectionURL() != null) pref.put("URL", desc.getConnectionURL());
		if (desc.getUserName() != null) pref.put("UserName", desc.getUserName());
		if (desc.getPassword() != null) pref.put("Password", desc.getPassword());
		pref.putBoolean("PromptPassword", desc.isPromptPassword());
		if (desc.getProperties() != null) PrefsUtils.setPreferences(desc.getProperties(), pref.node("Properties"));
	}

	/*******************************************************************************************************************
	 * Устанавливает Preferences из ConnectionElement
	 * 
	 * @param pref preferences to set
	 * @param elem connection element
	 ******************************************************************************************************************/
	public static void setPreferences(Preferences pref, ConnectionElement elem) {
		if (elem == null) throw new IllegalArgumentException("elem == null");
		if (pref == null) throw new IllegalArgumentException("pref == null");
		pref.put("Name", elem.getName());
		setPreferences(pref, elem.getDescriptor());
	}
}
